using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ScrollDaysMarathon : MonoBehaviour
{
	private List<ButAwardMarathon> listButton = new List<ButAwardMarathon>();

	public ButAwardMarathon exampleBut;

	public GameObject parentButton;

	private UIGrid gridScript;

	private SpringPanel curSpr;

	private void OnDestory()
	{
		if (curSpr != null)
		{
			SpringPanel springPanel = curSpr;
			springPanel.onFinished = (SpringPanel.OnFinished)Delegate.Remove(springPanel.onFinished, new SpringPanel.OnFinished(Sort));
		}
	}

	public void UpdateList(bool isSuperDay = false)
	{
		if (gridScript == null)
		{
			gridScript = parentButton.GetComponent<UIGrid>();
		}
		int num = 0;
		num = ((!isSuperDay) ? MarathonController.instance.listDaysMarathon.Count : MarathonController.instance.listSuperDaysMarathon.Count);
		SetButtonCount(num);
		if (isSuperDay)
		{
			for (int i = 0; i < listButton.Count; i++)
			{
				ButAwardMarathon butAwardMarathon = listButton[i];
				butAwardMarathon.SetInfo(MarathonController.instance.listSuperDaysMarathon[i], isSuperDay);
			}
		}
		else
		{
			for (int j = 0; j < listButton.Count; j++)
			{
				ButAwardMarathon butAwardMarathon2 = listButton[j];
				butAwardMarathon2.SetInfo(MarathonController.instance.listDaysMarathon[j], isSuperDay);
			}
		}
		Sort();
		Invoke("Sort", 0.2f);
	}

	private void Sort()
	{
		if (gridScript != null)
		{
			gridScript.repositionNow = true;
		}
		if (curSpr != null)
		{
			SpringPanel springPanel = curSpr;
			springPanel.onFinished = (SpringPanel.OnFinished)Delegate.Remove(springPanel.onFinished, new SpringPanel.OnFinished(Sort));
		}
	}

	public void CenterOnCurrentDay()
	{
		int countDayReceiveAward = MarathonController.instance.CountDayReceiveAward;
		if (countDayReceiveAward < listButton.Count)
		{
			StartCoroutine(CenterOnObj(listButton[countDayReceiveAward].gameObject));
		}
	}

	public ButAwardMarathon GetButAward(int numDay)
	{
		int num = numDay - 1;
		if (num < listButton.Count)
		{
			return listButton[num];
		}
		return null;
	}

	private IEnumerator CenterOnObj(GameObject needObj)
	{
		UIPanel panel = GetComponent<UIPanel>();
		UIScrollView sv = GetComponent<UIScrollView>();
		yield return new WaitForEndOfFrame();
		Vector3 offset = -panel.cachedTransform.InverseTransformPoint(needObj.transform.position);
		if (!sv.canMoveHorizontally)
		{
			offset.x = panel.cachedTransform.localPosition.x;
		}
		if (!sv.canMoveVertically)
		{
			offset.y = panel.cachedTransform.localPosition.y;
		}
		curSpr = SpringPanel.Begin(panel.cachedGameObject, offset, 60f);
		if (curSpr != null)
		{
			SpringPanel springPanel = curSpr;
			springPanel.onFinished = (SpringPanel.OnFinished)Delegate.Combine(springPanel.onFinished, new SpringPanel.OnFinished(Sort));
		}
	}

	private void SetButtonCount(int needCount)
	{
		if (listButton.Count < needCount)
		{
			for (int i = listButton.Count; i < needCount; i++)
			{
				listButton.Add(CreateButton());
			}
		}
		else if (listButton.Count > needCount)
		{
			int num = listButton.Count - needCount;
			for (int j = 0; j < num; j++)
			{
				GameObject obj = listButton[listButton.Count - 1].gameObject;
				listButton[listButton.Count - 1] = null;
				listButton.RemoveAt(listButton.Count - 1);
				UnityEngine.Object.Destroy(obj);
			}
		}
	}

	private ButAwardMarathon CreateButton()
	{
		GameObject gameObject = UnityEngine.Object.Instantiate(exampleBut.gameObject, Vector3.zero, Quaternion.identity) as GameObject;
		ButAwardMarathon component = gameObject.GetComponent<ButAwardMarathon>();
		gameObject.transform.parent = parentButton.transform;
		gameObject.SetActive(true);
		gameObject.transform.localScale = new Vector3(1f, 1f, 1f);
		return component;
	}
}
